home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / WINDOWS / WIN_MORT.ARJ / WINMORT.CPP < prev    next >
Text File  |  1992-01-12  |  4KB  |  189 lines

  1. /* WINMORT.CPP
  2. *    Description:    Basic Mortgage Calculator for Windows 3.0
  3. *
  4. *    Author:            Robert H. Kornberg
  5. *    Date:                1-03-91
  6. *
  7. */
  8.  
  9. #include <stdio.h>        // Standard "C" header files
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13.  
  14. #include <owl.h>            // Object Windows Header Files
  15. #include <edit.h>
  16. #include <static.h>
  17.  
  18. #include "moneycl.h"        // Header file for the MONEYCL class
  19.  
  20. #include "winmort.h"        // Header FIle containing Resource Ids
  21.  
  22. #define PRINSIZE    18
  23. #define TERMSIZE    3
  24. #define RATESIZE    7
  25. #define POINTSIZE    7
  26. #define PAYSIZE        18
  27. #define APRSIZE        7
  28.  
  29. typedef struct {                        // Dialog Box Transfer Buffer
  30.     char principle[PRINSIZE];
  31.     char term[TERMSIZE];
  32.     char rate[RATESIZE];
  33.     char points[POINTSIZE];
  34.     char payment[PAYSIZE];
  35.     char apr[APRSIZE];
  36. } MortFields;
  37.  
  38. // Main window containing the calculator fields
  39.  
  40. _CLASSDEF(TMortCalc)
  41. class TMortCalc : public TDialog
  42. {
  43.     MoneyCl MonthlyAmt, Principle;
  44.     double Rate, Points, APR;
  45.     int Term;
  46.     MortFields *FormData;
  47.     PTEdit    PPrinciple;
  48.     PTStatic PMonPay, PAPR;
  49. public:
  50.     TMortCalc (PTWindowsObject AParent);
  51.     ~TMortCalc();
  52.     virtual LPSTR GetClassName();
  53.     virtual void ProcessPrinciple(RTMessage Msg) = [ID_FIRST + id_Principle];
  54.     virtual void CalcMonthlyAmount() = [ID_FIRST + id_CalcBtn];
  55.     virtual void ClearAll() = [ID_FIRST + id_ClearBtn];
  56.     virtual void CalcAmounts();
  57.     virtual double Mort();
  58. };
  59.  
  60. TMortCalc::TMortCalc(PTWindowsObject AParent) : TDialog(AParent, MAKEINTRESOURCE(DIAG_MORTCALC))
  61. {
  62.     PPrinciple = new TEdit (this, id_Principle, PRINSIZE);
  63.     new TEdit (this, id_Term, TERMSIZE);
  64.     new TEdit (this, id_Rate, RATESIZE);
  65.     new TEdit (this, id_Points, POINTSIZE);
  66.     PMonPay = new TStatic (this, id_Monthly, PAYSIZE);
  67.     PAPR = new TStatic (this, id_APR, APRSIZE);
  68.  
  69.     FormData = new MortFields;
  70.     memset (FormData, 0, sizeof(MortFields));
  71.  
  72.     TransferBuffer = FormData;
  73.  
  74.     PMonPay->EnableTransfer();
  75.     PAPR->EnableTransfer();
  76.  
  77. }
  78.  
  79. TMortCalc::~TMortCalc()
  80. {
  81.     delete (PPrinciple);
  82.     delete (PMonPay);
  83.     delete (PAPR);
  84.  
  85.     delete (FormData);
  86. }
  87.  
  88. void TMortCalc::ClearAll()
  89. {
  90.     memset (FormData, 0, sizeof(MortFields));
  91.     TransferData (TF_SETDATA);
  92.  
  93.     PostMessage (HWindow, WM_NEXTDLGCTL, PPrinciple->HWindow, 1);
  94. }
  95.  
  96. LPSTR TMortCalc::GetClassName()
  97. {
  98.   return "BorDlg";
  99. }
  100.  
  101. void TMortCalc::CalcMonthlyAmount()
  102. {
  103.     TransferData (TF_GETDATA);
  104.  
  105.     Principle.SetValue( FormData->principle );
  106.     Rate = atof (FormData->rate)/100.0;
  107.     Points = atof (FormData->points)/100.0;
  108.     Term = atoi (FormData->term);
  109.  
  110.     APR = Rate;
  111.  
  112.     CalcAmounts();
  113.  
  114.     strcpy(FormData->payment, MonthlyAmt.MoneyStr());
  115.     sprintf(FormData->apr, "%.2lf", APR);
  116.  
  117.     TransferData (TF_SETDATA);
  118.  
  119.     PostMessage (HWindow, WM_NEXTDLGCTL, PPrinciple->HWindow, 1);
  120.  
  121. }
  122.  
  123. void TMortCalc::CalcAmounts()
  124. {
  125.     Points *= Principle.MoneyVal();
  126.     MonthlyAmt.SetValue(Mort());
  127.     Principle.SetValue( Principle.MoneyVal() - Points );
  128.  
  129.     double test, testpay, ifp;
  130.  
  131.     test = Rate;
  132.  
  133.     for (ifp = test; ifp <= 0.25; ifp += 0.0001)
  134.     {
  135.         APR = ifp;
  136.         testpay = Mort();
  137.         if (testpay >= MonthlyAmt.MoneyVal())
  138.             break;
  139.     }
  140.  
  141.     APR *= 100.0;
  142. }
  143.  
  144. double TMortCalc::Mort()
  145. {
  146.     return Principle.MoneyVal()/((1-pow(1+APR/12,-1*Term*12))/(APR/12));
  147. }
  148.  
  149. void TMortCalc::ProcessPrinciple(RTMessage Msg)
  150. {
  151.     if (  Msg.LP.Lo != 0 )
  152.         if ( Msg.LP.Hi == EN_KILLFOCUS)
  153.             {
  154.             char tempPrin[MONEYSIZE];
  155.  
  156.             PPrinciple->GetText(tempPrin, MONEYSIZE);
  157.             Principle.SetValue(tempPrin);
  158.             PPrinciple->SetText ( Principle.MoneyStr());
  159.             }
  160. }
  161.  
  162. // Standard OWL Application Class
  163.  
  164. class TMortApp : public TApplication
  165. {
  166. public:
  167.   TMortApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  168.     LPSTR lpCmdLine, int nCmdShow)
  169.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  170.   virtual void InitMainWindow();
  171. };
  172.  
  173. void TMortApp::InitMainWindow()
  174. {
  175.   MainWindow = new TMortCalc(NULL);
  176. }
  177.  
  178. // Program Entry Point
  179.  
  180. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  181.   LPSTR lpCmdLine, int nCmdShow)
  182. {
  183.   TMortApp PApp("Mortgage Calculator", hInstance, hPrevInstance,
  184.                lpCmdLine, nCmdShow);
  185.   PApp.Run();
  186.   return PApp.Status;
  187. }
  188.